package com.forestar.xzk.commonlibrary;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import java.util.Iterator;
import java.util.List;

/**
 * @author Administrator by xzk
 * @data 2020/7/23  15:55
 * @email APPUtils
 * @remark   app的设备常用工具类
 *
 */
public class APPUtils {

    public static boolean isServiceRunning(Context ctx, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
        if(servicesList == null || servicesList.size()==0){
            return false;
        }
        Iterator<ActivityManager.RunningServiceInfo> l = servicesList.iterator();
        while (l.hasNext()) {
            ActivityManager.RunningServiceInfo si = l.next();
            if (className.equals(si.service.getClassName())) {
                isRunning = true;
            }
        }
        return isRunning;
    }

    public static void finishService(Context ctx){
        for(ActivityManager.RunningServiceInfo rsi:getRunningService(ctx)){
            ComponentName serviceCMP = rsi.service;
            System.out.println(serviceCMP.getClassName());
            if(serviceCMP.getClassName().contains("com.mapzone.zmn.keeplive")){
                Intent intent = new Intent();
                intent.setComponent(serviceCMP);
                boolean isStop = ctx.stopService(intent);
                System.out.println(serviceCMP.getClassName()+":"+isStop);
            }
        }
        getRunningService(ctx);
    }

    public static List<ActivityManager.RunningServiceInfo> getRunningService(Context ctx) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> servicesList = activityManager.getRunningServices(Integer.MAX_VALUE);
        if(servicesList == null || servicesList.size()==0){
            return null;
        }
        for(ActivityManager.RunningServiceInfo rsi:servicesList){
            System.out.println(rsi.service.getClassName());
        }
//        Iterator<ActivityManager.RunningServiceInfo> l = servicesList.iterator();
//        while (l.hasNext()) {
//            ActivityManager.RunningServiceInfo si = l.next();
//            if (className.equals(si.service.getClassName())) {
//                isRunning = true;
//            }
//        }
        return servicesList;
    }
    public static boolean isRunningTaskExist(Context context, String processName){
        ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processList = am.getRunningAppProcesses();
        for(ActivityManager.RunningAppProcessInfo info:processList){
            if (info.processName.equals(processName)){
                return true;
            }
        }
        return false;
    }
}
